<template>
  <el-dialog
    :title="!dataForm.productId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="productName">
      <el-input v-model="dataForm.productName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="productTitle">
      <el-input v-model="dataForm.productTitle" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="productImg">
      <el-input v-model="dataForm.productImg" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="productCon">
      <el-input v-model="dataForm.productCon" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="productPrice">
      <el-input v-model="dataForm.productPrice" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="productSum">
      <el-input v-model="dataForm.productSum" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          productId: 0,
          productName: '',
          productTitle: '',
          productImg: '',
          productCon: '',
          productPrice: '',
          productSum: ''
        },
        dataRule: {
          productName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productTitle: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productImg: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productCon: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productPrice: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productSum: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.productId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.productId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/pmsproduct/info/${this.dataForm.productId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.productName = data.pmsProduct.productName
                this.dataForm.productTitle = data.pmsProduct.productTitle
                this.dataForm.productImg = data.pmsProduct.productImg
                this.dataForm.productCon = data.pmsProduct.productCon
                this.dataForm.productPrice = data.pmsProduct.productPrice
                this.dataForm.productSum = data.pmsProduct.productSum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/pmsproduct/${!this.dataForm.productId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'productId': this.dataForm.productId || undefined,
                'productName': this.dataForm.productName,
                'productTitle': this.dataForm.productTitle,
                'productImg': this.dataForm.productImg,
                'productCon': this.dataForm.productCon,
                'productPrice': this.dataForm.productPrice,
                'productSum': this.dataForm.productSum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
